var CustomList, OnlineCustom, AddCustom
var inst = new mdui.Dialog("#sureDeteCustom");


$(document).ready(function () {
    // var inst2 = new mdui.Select('#advList');
    CustomList = new Vue({
        el: '#customList',
        data: {
            custom: '',
            fanslist: 1,
            chatlist: [],
            check : false,
            checkList : [],
            aaa: true
        },
        created: function () {
            var _this = this
            getCustomList(function (res) {
                setPage('pages01', res, function (obj, first) {
                    // console.log(obj)
                    _this.custom = res.slice((obj.index - 1) * obj.limit, obj.index * obj.limit)
                })
                // _this.custom = res
                _this.custom.forEach(function(item){
                    item.check = _this.check;
                })
                // console.log(_this.custom)
                $("#loading").fadeOut();
            })
        },
        methods: {
            isenable: function (index) {
                this.custom[index].status = !this.custom[index].status;
                this.custom[index].status = this.custom[index].status ? 1 : 0;
                var that = this;
                $.get('/admin/custm_svc/isEnable', { id: this.custom[index].id, status: this.custom[index].status }, function (res) {
                    if(res.status == 1){
                        // var msg = '{"type":"isEnable","data":{"id":'+res.result.id+',"count":'+res.result.count+'}}';
                        // ws.send(msg);
                    }
                });
                // console.log(this.custom[index].status);
            },
            getFansList: function (id) {
                window.location.href = '/admin/chat_monitor/index.html?butt_id=' + id;
            },

            inviteCustom: function (data) {
                $("#invitePopup").fadeIn();
                $("#inviteCustomAccount").val(data.kf_account);
            },
            editCustom: function (res) {
                console.log(res)
                $("#editCustomAccount").val(res.username);
                $("#editCustomNick").val(res.nickname);
                $(".editCustomImg").attr('src', res.header_url)
                $("#editCustomMessage").fadeIn();

                // window.location.href = '/admin/custm_svc/edit.html'
            },
            deleteCustom: function (aaa) {
                var kf_account = aaa.split("@")[0]
                mdui.dialog({
                    content: '确认删除客服吗？',
                    buttons: [
                        {
                            text: '取消'
                        },
                        {
                            text: '确认',
                            onClick: function () {
                                console.log(kf_account)
                                deleteCustomMessage(kf_account)
                            }
                        }
                    ]
                });
            },
            confirmController: function () {
                $("#showConfirm").fadeIn();
            },
            clearPassword: function () {

                console.log(this.confirmShow)
                // iniPassword(function(res){
                //     console.log(res);
                // });
            },
            syncMsgInfo: function () {
                layer.confirm('确认操作？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                    , btn3: function (index, layero) {
                        //按钮【按钮三】的回调
                        layer.closeAll();
                    }
                }, function (index, layero) {
                    //按钮【按钮一】的回调
                    layer.load();
                    getHistoryMsg(function (res) {
                        layer.closeAll();
                        layer.msg(res.msg);
                    });
                });
            },
            defaultReply : function(id){
                layer.open({
                    type : 2,
                    content : '/admin/custm_svc/reply/id/' + id,
                    area : ['400px','600px'],
                    btn: ['确认'],
                    yes: function(index, layero){
                        //按钮【按钮一】的回调
                        var inputForm = $(window.frames["layui-layer-iframe" + index].document).contents().find("#dasdasdasd");
                        var d = {};
                        $(inputForm).serializeArray().forEach(function(item){
                            d[item.name] = item.value
                        });
                        saveReplyMsg(id,d,function(res){
                            layer.closeAll();
                            if(res.status == 1){
                                layer.msg(res.msg)
                            }else{
                                layer.msg(res.error)
                            }
                            
                        });
                        // console.log(d);
                    }
                });
            },
            checkAll : function(){
                this.aaa = !this.aaa
                var that = this;
                that.check = !that.check
                var checkList = []
                that.custom.forEach(function(item){
                    item.check = that.check
                    console.log(item.check)
                    // checkList.push(item.id);
                })
                console.log(that.custom)
                // that.checkList = checkList;
                // console.log();
            },
            seleTag : function(){
                console.log(1);
            },
            setRemark : function(data){
                var remark = '';
                if(data.remark){
                    remark = data.remark;
                }

                layer.open({
                    type : 1,
                    content : '<div style="width:350px;height:150px;"><textarea id="tttt" style="width:330px;height:130px;padding:8px;border:none;resize:none;" placeholder="请输入备注">'+remark+'</textarea></div>',
                    btn: ['确认'],
                    yes: function(index, layero){
                        //按钮【按钮一】的回调
                        var content = $(layero).find("#tttt").val();
                        setCsRemark(data.id,content,function(res){
                            if(res.status == 1){
                                layer.msg('修改成功',{time:2000},function(){
                                    window.location.reload();
                                });
                            }else if(res.status == -1){
                                layer.msg('修改失败,请重新编辑');
                            }
                        });
                    }
                });
            },
            adv_list : function (){
                console.log(1);
            },
            extPosters : function(id){
                layer.open({
                    type : 2,
                    content : '/admin/custm_svc/poster/id/' + id,
                    area : ['400px','800px'],
                    btn: ['确认生成'],
                    yes: function(index, layero){
                        //按钮【按钮一】的回调
                        var inputForm = $(window.frames["layui-layer-iframe" + index].document).contents().find("#dasdasdasd");
                        var d = {};
                        $(inputForm).serializeArray().forEach(function(item){
                            d[item.name] = item.value
                        });

                        console.log(d);
                        savePoster(id,d,function(res){
                            layer.closeAll();
                            if(res.status == 1){
                                layer.msg(res.msg)
                            }else{
                                layer.msg(res.error)
                            }
                            
                        });
                        // console.log(d);
                    }
                });
            },
        }
    });

    OnlineCustom = new Vue({
        el: '#onlineCustom',
        data: {
            onlineCustomList: ''
        },
        created: function () {
            var that = this;
            getOnlineCustom(function (res) {
                setPage('pages02', res.kf_online_list, function (obj, first) {
                    console.log(obj)
                    that.onlineCustomList = res.kf_online_list.slice((obj.index - 1) * obj.limit, obj.index * obj.limit)
                })
            })

        }
    });

    AddCustom = new Vue({
        el: '#addCustom',
        data:{
            adv_list:[],
            cs_list:[],
        },
        created:function () {
            var that = this;
            getAdvList(function(res){
                console.log(res.qr_list);
                that.adv_list = res.qr_list;
                
                console.log(that.adv_list)
            });
            getPersonCsList(function(res){
                that.cs_list = res.cs_list;
                console.log(that.cs_list);
            })
        },
        updated:function(){
            let inst2 = new mdui.Select('#advList');
            let inst3 = new mdui.Select('#csList');
                inst2.handleUpdate();
                inst3.handleUpdate();
        }
    });

})